﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using RibbonEditor.Templates;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor._Global
{
    class Templates: IXmlBuildable, IChildDeletable
    {
        public List<RibbonTemplates> RibbonTemplatesList;

        public Templates()
        {
            this.RibbonTemplatesList = new List<RibbonTemplates>();
        }

        public Templates(XmlNode node)
            : this()
        {
            try
            {
                foreach (XmlNode cmdXmlNode in node.SelectNodes("RibbonTemplates"))
                {
                    if (cmdXmlNode.NodeType == XmlNodeType.Comment)
                        return;

                    RibbonTemplates rTemplates = new RibbonTemplates(cmdXmlNode);
                    this.RibbonTemplatesList.Add(rTemplates);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ribbonDiffXmlNode)
        {
            try
            {
                XmlNode commandDefinitionsXmlNode = XmlHelper.AddNode(ribbonDiffXmlNode, "Templates");

                foreach (RibbonTemplates rTemplates in this.RibbonTemplatesList)
                {
                    rTemplates.AddNode(commandDefinitionsXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode templatesNode = new TreeNode("Templates");
                templatesNode.ContextMenuStrip = this.BuildContextMenu(templatesNode);
                templatesNode.Tag = this;


                foreach (RibbonTemplates rTemplates in this.RibbonTemplatesList)
                {
                    rTemplates.BuildTreeViewElement(templatesNode);
                }

                parentNode.Nodes.Add(templatesNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Ribbon Templates");
            tsmi.Click += new EventHandler(tsmi_Click);
            cms.Items.Add(tsmi);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);

            return cms;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                RibbonTemplates rTemplates = new RibbonTemplates();
                rTemplates.BuildTreeViewElement(selectedNode);

                this.RibbonTemplatesList.Add(rTemplates);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is RibbonTemplates)
                {
                    this.RibbonTemplatesList.Remove((RibbonTemplates)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
